package com.briup.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * mybatisplus 配置类
 * @Author briup-teacher
 * @Date 2023-11
 **/
@MapperScan(basePackages = "com.briup.mapper")
@Configuration
public class MybatisplusConfig {
    //@Bean 创建id生成器
    @Bean
    public IdentifierGenerator idGenerator() {
        return new CustomIdGenerator();
    }

    /**
     * 添加分页插件
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));//如果配置多个插件,切记分页最后添加
        //interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); 如果有多数据源可以不配具体类型 否则都建议配上具体的DbType
        return interceptor;
    }

}

/**
 * 用户自定义的id生成器
 */
class CustomIdGenerator implements IdentifierGenerator{

    @Override
    public boolean assignId(Object idValue) {
        //是否生成对应的id值
        return IdentifierGenerator.super.assignId(idValue);
    }

    @Override
    public Number nextId(Object entity) {
        //生成一个数字id
        System.out.println("生成一个数字id");
        return Math.random();
    }

    @Override
    public String nextUUID(Object entity) {
        System.out.println("生成一个字符串id");
        return "briup"+System.currentTimeMillis();
    }
}

